﻿Public Class frmOpenPit

    Private m_Control As CAermod.Control
    Private m_Source As CAermod.Source
    Private m_OutPutOption As CAermod.OutPuts
    Private m_Receptor As CAermod.Receptor
    Property Control() As CAermod.Control
        Get
            Return m_Control
        End Get
        Set(ByVal value As CAermod.Control)
            m_Control = value
        End Set
    End Property
    Property Source() As CAermod.Source
        Get
            Return m_Source
        End Get
        Set(ByVal value As CAermod.Source)
            m_Source = value
        End Set
    End Property
    Property OutPutOption() As CAermod.OutPuts
        Get
            Return m_OutPutOption
        End Get
        Set(ByVal value As CAermod.OutPuts)
            m_OutPutOption = value
        End Set
    End Property
    Property Receptor() As CAermod.Receptor
        Get
            Return m_Receptor
        End Get
        Set(ByVal value As CAermod.Receptor)
            m_Receptor = value
        End Set
    End Property


    Private Sub frmopenpit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.FlexGrid.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)
        Me.MergeFlexGrid() '合并单元表格
        Me.FlexGrid.Rows.Count = Me.m_Source.OpenPitSources.OpenPitSource.Length + 2
        RefreshFlexData()

        Me.FlexGrid.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.FlexGrid.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.FlexGrid.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None
    End Sub

    ''' <summary>
    ''' 合并单元表格
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub MergeFlexGrid()
        Me.FlexGrid.Cols.Count = 11
        Me.FlexGrid.Styles.Normal.WordWrap = True
        Me.FlexGrid.AllowMerging = C1.Win.C1FlexGrid.AllowMergingEnum.FixedOnly
        Me.FlexGrid.Rows(0).AllowMerging = True
        Dim rng As C1.Win.C1FlexGrid.CellRange = Me.FlexGrid.GetCellRange(0, 0, 1, 0)
        rng.Data = "序号"
        rng = Me.FlexGrid.GetCellRange(0, 1, 1, 1)
        rng.Data = "编号"
        rng = Me.FlexGrid.GetCellRange(0, 2, 1, 2)
        rng.Data = "污染源名称"
        rng = Me.FlexGrid.GetCellRange(0, 3, 0, 5)
        rng.Data = "面源顶点坐标"
        Me.FlexGrid.SetData(1, 3, "Xs[m]")
        Me.FlexGrid.SetData(1, 4, "Ys[m]")
        Me.FlexGrid.SetData(1, 5, "Zs[m]")
        rng = Me.FlexGrid.GetCellRange(0, 6, 0, 10)
        rng.Data = "面源参数"
        Me.FlexGrid.SetData(1, 6, "高度[m]")
        Me.FlexGrid.SetData(1, 7, "X边长[m]")
        Me.FlexGrid.SetData(1, 8, "Y边长[m]")
        Me.FlexGrid.SetData(1, 9, "方向角[度]")
        Me.FlexGrid.SetData(1, 10, "体积[m^3]")

        MergeSource() '合并污染物
        'HostControl() '设置表格在控件中
        Me.FlexGrid.AutoSizeCols()
    End Sub
    Private Sub MergeSource()
        Me.FlexGrid.Cols.Count = Me.FlexGrid.Cols.Count + Me.m_Control.AllPolluts.AllSelectPollut.Length + 1
        Dim rng As C1.Win.C1FlexGrid.CellRange = Me.FlexGrid.GetCellRange(0, 11, 0, Me.FlexGrid.Cols.Count - 1)
        rng.Data = "污染物排放速率"
        Dim i As Integer
        For i = 0 To Me.m_Control.AllPolluts.AllSelectPollut.Length - 1
            Me.FlexGrid.SetData(1, i + 11, Me.m_Control.AllPolluts.AllSelectPollut(i).Name)
        Next
        Me.FlexGrid.SetData(1, Me.m_Control.AllPolluts.AllSelectPollut.Length + 11, "    单位")
        Me.FlexGrid.Cols(Me.m_Control.AllPolluts.AllSelectPollut.Length + 11).ComboList = "g/s•m^2|g/s|kg/h"
        Me.FlexGrid.Cols.Count = Me.FlexGrid.Cols.Count + 1
        Me.FlexGrid.Cols(Me.FlexGrid.Cols.Count - 1).AllowMerging = True
        Me.FlexGrid.Cols(Me.FlexGrid.Cols.Count - 1).AllowEditing = False
        Me.FlexGrid.Cols(Me.FlexGrid.Cols.Count - 1).TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter
        rng = Me.FlexGrid.GetCellRange(0, Me.FlexGrid.Cols.Count - 1, 1, Me.FlexGrid.Cols.Count - 1)
        rng.Data = "详细设置"
        Me.FlexGrid.Styles.Fixed.TextAlign = C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter

        For i = 0 To Me.FlexGrid.Cols.Count - 1
            Me.FlexGrid.Cols(i).AllowDragging = False
            Me.FlexGrid.Cols(i).AllowSorting = False
            Me.FlexGrid.Cols(i).AllowResizing = False
        Next
    End Sub
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        UpdateData()
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub cmdDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDel.Click
        Dim iSelected As Integer = Me.FlexGrid.RowSel
        If iSelected > 1 Then
            Me.FlexGrid.DelRow()
            If iSelected <= Me.FlexGrid.Rows.Count - 1 Then
                For i As Integer = iSelected To Me.FlexGrid.Rows.Count - 1
                    Me.m_Source.OpenPitSources.OpenPitSource(i - 2) = Me.m_Source.OpenPitSources.OpenPitSource(i - 1)
                Next
            End If
            ReDim Preserve Me.m_Source.OpenPitSources.OpenPitSource(Me.m_Source.OpenPitSources.OpenPitSource.Length - 2)
            RefreshFlexData()
        End If
    End Sub

    Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
        Me.FlexGrid.AddRow()
        ReDim Preserve Me.m_Source.OpenPitSources.OpenPitSource(Me.m_Source.OpenPitSources.OpenPitSource.Length)
        Me.m_Source.OpenPitSources.OpenPitSource(Me.m_Source.OpenPitSources.OpenPitSource.Length - 1) = New CAermod.OpenPitSource
        ReDim Me.m_Source.OpenPitSources.OpenPitSource(Me.m_Source.OpenPitSources.OpenPitSource.Length - 1).PollutionPara(Me.m_Control.AllPolluts.AllSelectPollut.Length - 1) '设置新增加的污染源的污染物的类型
        Me.m_Source.OpenPitSources.OpenPitSource(Me.m_Source.OpenPitSources.OpenPitSource.Length - 1).PollutionPara(Me.m_Control.AllPolluts.AllSelectPollut.Length - 1) = New CAermod.PollutionPara
        Me.m_Source.OpenPitSources.OpenPitSource(Me.m_Source.OpenPitSources.OpenPitSource.Length - 1).SetPollutePara(AermodProject0.Aermod.Control.AllPolluts)
        '刷新数据
        Me.m_Source.OpenPitSources.OpenPitSource(Me.m_Source.OpenPitSources.OpenPitSource.Length - 1).Location.Srcid = "OP" & (Me.m_Source.OpenPitSources.nStack + 1)
        Me.m_Source.OpenPitSources.nStack += 1
        Me.m_Source.OpenPitSources.OpenPitSource(Me.m_Source.OpenPitSources.OpenPitSource.Length - 1).SourceName = "敞口源" & Me.m_Source.OpenPitSources.nStack '污染源名称

        RefreshFlexData()
    End Sub
    ''' <summary>
    ''' 用污染源对象的数据更新表格的数据
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub RefreshFlexData()
        Me.FlexGrid.Rows.Count = Me.m_Source.OpenPitSources.OpenPitSource.Length + 2
        For i As Integer = 0 To Me.m_Source.OpenPitSources.OpenPitSource.Length - 1
            Me.FlexGrid.SetData(i + 2, 0, (i + 1).ToString) '序号
            Me.FlexGrid.SetData(i + 2, 1, Me.m_Source.OpenPitSources.OpenPitSource(i).Location.Srcid) '编号
            Me.FlexGrid.SetData(i + 2, 2, Me.m_Source.OpenPitSources.OpenPitSource(i).SourceName) '污染源名称
            Me.FlexGrid.SetData(i + 2, 3, Me.m_Source.OpenPitSources.OpenPitSource(i).Location.Xs) '污染源底坐标
            Me.FlexGrid.SetData(i + 2, 4, Me.m_Source.OpenPitSources.OpenPitSource(i).Location.Ys) '污染源底坐标
            Me.FlexGrid.SetData(i + 2, 5, Me.m_Source.OpenPitSources.OpenPitSource(i).Location.Zs) '污染源底坐标
            Me.FlexGrid.SetData(i + 2, 6, Me.m_Source.OpenPitSources.OpenPitSource(i).Relhgt) '面源的高度
            Me.FlexGrid.SetData(i + 2, 7, Me.m_Source.OpenPitSources.OpenPitSource(i).Xinit) 'X边长
            Me.FlexGrid.SetData(i + 2, 8, Me.m_Source.OpenPitSources.OpenPitSource(i).Yinit) 'Y边长
            Me.FlexGrid.SetData(i + 2, 9, Me.m_Source.OpenPitSources.OpenPitSource(i).Angle) '方向角
            Me.FlexGrid.SetData(i + 2, 10, Me.m_Source.OpenPitSources.OpenPitSource(i).Pitvol) '体积
            For j As Integer = 0 To Me.m_Source.OpenPitSources.OpenPitSource(i).PollutionPara.Length - 1 '设置不同类型的污染物及对应的排放速率
                Me.FlexGrid.SetData(i + 2, 11 + j, Me.m_Source.OpenPitSources.OpenPitSource(i).PollutionPara(j).Ptemis)
            Next
            Me.FlexGrid.SetData(i + 2, 11 + Me.m_Source.OpenPitSources.OpenPitSource(i).PollutionPara.Length, Me.m_Source.OpenPitSources.OpenPitSource(i).PolluteUnit)
            Me.FlexGrid.SetData(i + 2, Me.FlexGrid.Cols.Count - 1, "设置")
        Next
        Me.FlexGrid.AutoSizeCols()
    End Sub
    ''' <summary>
    ''' 用表格中的数据更新对象中的数据
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub UpdateData()
        For i As Integer = 0 To Me.m_Source.OpenPitSources.OpenPitSource.Length - 1
            Me.m_Source.OpenPitSources.OpenPitSource(i).Location.Srcid = Me.FlexGrid.GetData(i + 2, 1) '编号
            Me.m_Source.OpenPitSources.OpenPitSource(i).SourceName = Me.FlexGrid.GetData(i + 2, 2) '污染源名称
            Me.m_Source.OpenPitSources.OpenPitSource(i).Location.Xs = Me.FlexGrid.GetData(i + 2, 3) '污染源底坐标
            Me.m_Source.OpenPitSources.OpenPitSource(i).Location.Ys = Me.FlexGrid.GetData(i + 2, 4) '污染源底坐标
            Me.m_Source.OpenPitSources.OpenPitSource(i).Location.Zs = Me.FlexGrid.GetData(i + 2, 5) '污染源底坐标
            Me.m_Source.OpenPitSources.OpenPitSource(i).Relhgt = Me.FlexGrid.GetData(i + 2, 6) '高度
            Me.m_Source.OpenPitSources.OpenPitSource(i).Xinit = Me.FlexGrid.GetData(i + 2, 7) 'X边长
            Me.m_Source.OpenPitSources.OpenPitSource(i).Yinit = Me.FlexGrid.GetData(i + 2, 8) 'Y边长
            Me.m_Source.OpenPitSources.OpenPitSource(i).Angle = Me.FlexGrid.GetData(i + 2, 9) '方向角
            Me.m_Source.OpenPitSources.OpenPitSource(i).Pitvol = Me.FlexGrid.GetData(i + 2, 10) '体积
            For j As Integer = 0 To Me.m_Source.OpenPitSources.OpenPitSource(i).PollutionPara.Length - 1 '不同类型的污染物及对应的排放速率
                Me.m_Source.OpenPitSources.OpenPitSource(i).PollutionPara(j).Ptemis = Me.FlexGrid.GetData(i + 2, 11 + j)
            Next
            '对数据进行转换
            Me.m_Source.OpenPitSources.OpenPitSource(i).PolluteUnit = Me.FlexGrid.GetData(i + 2, 11 + Me.m_Source.OpenPitSources.OpenPitSource(i).PollutionPara.Length)
            Me.m_Source.OpenPitSources.OpenPitSource(i).AngleToTop()
        Next
        Me.FlexGrid.AutoSizeCols()
    End Sub

    Private Sub FlexGrid_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles FlexGrid.AfterEdit
        UpdateData()
    End Sub

    Private Sub FlexGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlexGrid.Click

    End Sub

    Private Sub FlexGrid_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles FlexGrid.DoubleClick
        If Me.FlexGrid.ColSel = Me.FlexGrid.Cols.Count - 1 AndAlso Me.FlexGrid.Clip = "设置" Then
            Dim dlgOpenPit As New dlgOpenPitSource
            Dim ps As New CAermod.OpenPitSources
            ps = Me.m_Source.OpenPitSources.Clone
            dlgOpenPit.BaseSoruce = ps.OpenPitSource
            dlgOpenPit.Urbans = Me.m_Control.Urbans.Clone
            dlgOpenPit.IntialSelectedIndex = Me.FlexGrid.RowSel - 2
            dlgOpenPit.ArrayPollut = Me.m_Control.AllPolluts.AllSelectPollut.Clone  '所有选中的污染物
            dlgOpenPit.Control = Me.m_Control.Clone
            dlgOpenPit.SourceCount = Me.m_Source.OpenPitSources.nStack
            If dlgOpenPit.ShowDialog = Windows.Forms.DialogResult.OK Then
                ps.OpenPitSource = CType(dlgOpenPit.BaseSoruce, CAermod.OpenPitSource())
                Me.m_Source.OpenPitSources = ps.Clone
                Me.m_Source.OpenPitSources.nStack = dlgOpenPit.SourceCount
                RefreshFlexData() '刷新表格中的数据
            End If
        End If
    End Sub

    Private Sub cmdControl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdControl.Click
        Dim frmControl As New frmControl
        frmControl.UltraTabControl2.Tabs(1).Visible = False
        frmControl.Control = Me.m_Control.Clone
        frmControl.Receptor = Me.m_Receptor.Clone
        frmControl.Source = Me.m_Source.Clone
        frmControl.OutPutOption = Me.m_OutPutOption.Clone
        If frmControl.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.m_Control = frmControl.Control.Clone
            Me.m_Receptor = frmControl.Receptor.Clone
            Me.m_Source = frmControl.Source.Clone
            Me.m_OutPutOption = frmControl.OutPutOption.Clone
            '更新数据
            Me.MergeFlexGrid() '合并单元表格
            Me.FlexGrid.Rows.Count = Me.m_Source.OpenPitSources.OpenPitSource.Length + 2
            RefreshFlexData()
        End If
    End Sub
End Class